//
//  YbrTimerUtil.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-4.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrTimerUtil.h"
#import "YbrMyConfig.h"

@implementation YbrTimerUtil
+(NSInteger) makeTimeInt:(NSString *) time {
    NSRange range = [time rangeOfString:@":"];
  
    if(time == nil || range.length == 0) {
        return 0;
    }

    NSArray *array = [time componentsSeparatedByString:@":"];
    if([array count] > 3) {
        return 0;
    }
    
    NSInteger timeint = 0;
    
    for (int i = 0; i < [array count]; i ++) {
        timeint += pow(60, [array count] - i - 1) * [array[i] intValue];
    }
    
    return timeint;
}

+(NSString *) makeTimeStr:(int) time {
    NSString *myTime;
    
    int time1 = time / 60;
    int time2 = time % 60;
    
    NSString *time1Str ,*time2Str;
    
    if (time1 >= 10) {
        time1Str = [NSString stringWithFormat:@"%i" ,time1];
    }
    else {
        time1Str = [NSString stringWithFormat:@"0%i" ,time1];
    }
    
    if (time2 >= 10) {
        time2Str = [NSString stringWithFormat:@"%i" ,time2];
    }else {
        time2Str = [NSString stringWithFormat:@"0%i" ,time2];
    }
    
    myTime = [NSString stringWithFormat:@"%@:%@" ,time1Str ,time2Str];
    return myTime;
}

// 这里的time是以秒为计算单位的 (1小时 = 3600秒  1分钟 = 60秒)
+(NSString *) makeClockChooseTimeStr:(int) time {
    NSString *myTime;
    
    int time0 = time / 3600;    // 小时的
    
    int minTime = time % 3600;    // 剩余的
    
    int time1 = minTime / 60;   // 分钟的
    int time2 = minTime % 60;   // 秒的
    
    NSString *time0Str, *time1Str ,*time2Str;
    
    if (time0 >= 10) {
        time0Str = [NSString stringWithFormat:@"%i" ,time0];
    }
    else {
        time0Str = [NSString stringWithFormat:@"0%i" ,time0];
    }
    
    if (time1 >= 10) {
        time1Str = [NSString stringWithFormat:@"%i" ,time1];
    }
    else {
        time1Str = [NSString stringWithFormat:@"0%i" ,time1];
    }
    
    if (time2 >= 10) {
        time2Str = [NSString stringWithFormat:@"%i" ,time2];
    }else {
        time2Str = [NSString stringWithFormat:@"0%i" ,time2];
    }
    
    myTime = [NSString stringWithFormat:@"%@:%@:%@" , time0Str , time1Str ,time2Str];
    return myTime;
}


+(NSArray *) getAllWeekDay {
    NSArray *nsarr = [NSArray arrayWithObjects:@"星期一" ,@"星期二" ,@"星期三" ,@"星期四" ,@"星期五" ,@"星期六" ,@"星期日", nil];
    return nsarr;
}

+(NSString *) getWeekDay:(int) whichDay {
    NSArray *weekArr = [self getAllWeekDay];
    NSString *weekDayName = weekArr[whichDay];
    return weekDayName;
}

// 检测是够重叠, 返回-1表示没有重叠
+(int) overLap:(NSArray *) begins ends:(NSArray *)ends beginTime:(int) beginTime endTime:(int)endTime {
    for (int i = 0; i < begins.count; i ++) {
        if(!(([begins[i] intValue] > endTime) || ([ends[i] intValue] < beginTime))) {
            if([begins[i] intValue] == [self getConfig].realBeginTime && [ends[i] intValue] == [self getConfig].realEndTime) {
                return -1;
            }
            return i;
        }
    }
    return -1;
}

+(NSArray *) getOverlist:(NSArray *) begins ends:(NSArray *)ends beginTime:(int) beginTime endTime:(int)endTime {
    NSMutableArray *overList = [NSMutableArray array];
    for (int i = 0; i < begins.count; i ++) {
        if(!(([begins[i] intValue] > endTime) || ([ends[i] intValue] < beginTime))) {
            if([begins[i] intValue] == [self getConfig].realBeginTime && [ends[i] intValue] == [self getConfig].realEndTime) {
                return nil;
            }
            [overList addObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:[begins[i] intValue]], [NSNumber numberWithInt:[ends[i] intValue]], nil]];
        }
    }
    return [overList copy];
}

+(YbrMyConfig *) getConfig {
    return [YbrMyConfig myConfigInit];
}

@end
